// pages/apply/evaluate/index.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		imgQingdan: '',
		imgFaoiao:'',
		imgFaoiao_name:'',
		arr:'',
		id:'',
		fpType:1,//1纸发票 2电子发票
		baseUrl:app.globalData.baseUrl
	},
	onPreview(e){
		console.log(e.currentTarget.dataset.src)
		// var url =  e
		wx.previewImage({
		  current: e.currentTarget.dataset.src, // 当前显示图片的 http 链接
		  urls: [e.currentTarget.dataset.src] // 需要预览的图片 http 链接列表
		})
	},
	onGetData(id){
		  let that = this;
		  wx.showLoading({
		  	title: '加载中',
		  	mask: true
		  })
		  wx.request({
		  	url: app.globalData.baseUrl + '/api/apply/detail',
		  	method: 'GET',
		  	header: {
		  		'content-type': 'application/x-www-form-urlencoded',
		  		'token': app.globalData.token,
		  	},
			data:{
				id:id
			},
		  	success: function(res) {
		  		wx.hideLoading()
		  		if (res.data.status) {
					if(res.data.data.records.img_urls){
						res.data.data.records.img_urls = res.data.data.records.img_urls.split(',')
					}
					
					that.setData({
						data:res.data.data.records,
						imgQingdan:	res.data.data.records.ding_sun_url,
						imgFaoiao:res.data.data.records.invoice_url,
						imgFaoiao_name:res.data.data.records.invoice_url?res.data.data.records.invoice_url.split('/')[res.data.data.records.invoice_url.split('/').length-1]:''
					})
					
		  		}
	
		  	
		  	},
		  	fail: function() {
		  		wx.hideLoading()
		  		console.log("请求失败")
		  	}
		  })
	},
	checkboxChange(e){
		this.setData({
			fpType:e.detail.value
		})
		    
		console.log(e.detail.value)
	},
	onGetaddr() {
		let that = this;
		wx.request({
			url: app.globalData.baseUrl + '/api/apply/sendInvoiceAddress',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},

			success: function(res) {
			
				wx.hideLoading()
				if (res.data.status) {
					that.setData({
						addr:res.data.data.records
					})
					
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000,
					})
				}
				console.log(that.data.addr[0].val)
			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	onSubmit(e) {

		var that = this;
		wx.showLoading({
			title: '提交中',
			mask: true
		})
		wx.request({
			url: app.globalData.baseUrl + '/api/apply/selfRepair',
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			data: {
				"order_id": that.data.id,
				"bank_account_name":e.detail.value.bank_account_name,
				"bank_type": e.detail.value.bank_type,
				"bank_card":e.detail.value.bank_card,
				"repair_list_img_url":that.data.imgQingdan,
				"invoice_url": that.data.imgFaoiao,
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					wx.showModal({
						title: "提示",
						content:'提交成功',
						icon: 'none',
						showCancel: false,
						confirmColor: '#DC5640',
						success(res) {
							wx.navigateBack()
						}
					})

				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000,
					})
				}
			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	onUploadGuard(file,type) {
		let that = this;
		wx.showLoading({
			title: '上传中',
			mask: true
		})
		wx.uploadFile({
			url: app.globalData.baseUrl + '/guard/upload', //仅为示例，非真实的接口地址
			method: 'POST', //这句话好像可以不用
			header: {
				'Content-Type': 'Application/x-www-form-urlencoded;charset=utf-8',
				'token': app.globalData.token,
			},
			filePath: file,
			name: 'file',
			formData: {
				"module": "factory"
			},
			success(res) {
				wx.hideLoading()
				let data = JSON.parse(res.data)
				if (data.status) {
					if(type == 1){
						that.setData({
							imgQingdan: data.data.records.filePath
						})
					}else if(type == 2){
						that.setData({
							imgFaoiao: data.data.records.filePath,
							imgFaoiao_name: data.data.records.filePath? data.data.records.filePath.split('/')[ data.data.records.filePath.split('/').length-1]:''
						})
					}
				} else {
					wx.showToast({
						title: data.msg,
						icon: 'none',
						duration: 2000
					})
				}

				//do something
			}
		})
		return false;

	},

	choseImage1(e) {

		let that = this;
		wx.chooseImage({
			count: 1,
			mediaType: ['image'],
			sourceType: ['album', 'camera'],
			maxDuration: 30,
			camera: 'back',
			success(res) {
				let size = res.tempFiles[0].size;
				if (size > 1024 * 1024 * 2) {
					wx.showToast({
						title: '文件过大',
						icon: 'none',
						duration: 2000
					})
					return false
				}
				that.onUploadGuard(res.tempFiles[0].path, 1)


			}
		})
	},
	choseImage2(e) {
		let that = this;
		wx.chooseMessageFile({
			count: 1,
			type: 'all',
			success(res) {
				let size = res.tempFiles[0].size;
				let fileType = res.tempFiles[0].name.split('.')[res.tempFiles[0].name.split('.').length - 1];
				if(fileType != 'pdf'){
					wx.showToast({
						title: '请上传pdf文件',
						icon: 'none',
						duration: 2000
					})
					return false
				}
				if (size > 1024 * 1024 * 2) {
					wx.showToast({
						title: '文件过大',
						icon: 'none',
						duration: 2000
					})
					return false
				}
				that.onUploadGuard(res.tempFiles[0].path, 2)
	
	
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			id:options.id
		})
		console.log()
		this.onGetaddr();
		if(options.type == 'again'){
			this.onGetData(options.id);
		}
		
		
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
